$(function () {

    // TODO 鼠标的移入移出
    $(".collected").mouseover(function () {
        $(this).html('取消收藏')
    }).mouseout(function () {
        $(this).html('已收藏&nbsp;&nbsp;')
    });


    // TODO 收藏
    $(".collection").click(function () {
        var goods_id = $(this).attr('data-goodsid');
        var action = 'collect'

        params = {
            'goods_id': goods_id,
            'action': action
        }
        $.ajax({
            url: "/goods/goods_collect",
            type: "post",
            contentType: "application/json",
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            data: JSON.stringify(params),
            success: function (data) {
                if (data.errno == "0") {
                    // 收藏成功
                    // 隐藏收藏按钮
                    $(".collection").hide();
                    // 显示取消收藏按钮
                    $(".collected").show();
                } else if (data.errno == "4101") {
                    alert('请登录')
                    location.href = '/passport/login'
                } else {
                    alert(data.errmsg);
                }
            }
        })

    })

    // TODO 取消收藏
    $(".collected").click(function () {
        var goods_id = $(this).attr('data-goodsid');
        var action = 'cancel_collect'


        params = {
            'goods_id': goods_id,
            'action': action
        }
        $.ajax({
            url: "/goods/goods_collect",
            type: "post",
            contentType: "application/json",
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            data: JSON.stringify(params),
            success: function (data) {
                if (data.errno == "0") {
                    // 取消收藏成功
                    // 隐藏收藏按钮
                    $(".collection").show();
                    // 显示取消收藏按钮
                    $(".collected").hide();
                } else if (data.errno == "4101") {
                    alert('请登录')
                    location.href = '/passport/login'
                } else {
                    alert(data.errmsg);
                }
            }
        })

    })
    // TODO 加入购物车
    $(".shop_car").click(function () {
        var goods_id = $('.goods_id').html();
        var goods_count = $('#text_box').val();

        params = {
            'goods_id': goods_id,
            'goods_count': goods_count
        }
        $.ajax({
            url: "/goods/shop_car",
            type: "post",
            contentType: "application/json",
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            data: JSON.stringify(params),
            success: function (data) {
                if (data.errno == '0') {
                    alert(data.errmsg)
                } else if (data.errno == '8181') {
                    alert(data.errmsg);
                    location.href = '/passport/login'
                } else {
                    alert(data.errmsg)
                }
            }, error() {
                alert('系统繁忙,请稍后再试')
            }

        })

    })
    $(".buying").click(function () {
        var goods_id = $('.goods_id').html();
        var g_count = $('#text_box').val();
        $('.buying').prop(
            {
                'href': '/goods/order?g_id=' + goods_id + '&&g_count=' + g_count,
            }
        )

    })

})
function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}